import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class MaintainBlueprintService {
  ORDERSTATUS: any = {
    0: '待付款',
    1: '开通中',
    2: '交易完成',
    3: '支付超时',
    4: '客服取消',
    5: '交易结束',
  };
  PAYSTATUS: any = {
    0: '未付款',
    1: '已付款',
    2: '部分退款',
    3: '全部退款',
  };
  PAYTYPE: any = {
    1: '支付宝',
    2: '微信',
    3: '银联',
  };
  serviceUrls: any = {
    dictUrl: '/dict/detail', // 字典查询
    uploadFileUrl: '/common/upload/file', // 上传
    memberList: '/jc/order/list', // 会员开通记录
    memberCancel: '/jc/order/cancel', // 会员开通记录取消订单
    memberDetail: '/jc/order/info/', // 会员开通记录详情
    memberExport: '/jc/order/export', // 会员开通记录导出
    memberRefund: '/jc/order/refund', // 会员开通记录售后退款
    memberTrack: '/jc/order/follow', // 会员开通记录跟进信息
  };

  constructor(public httpClient: HttpClient) {
  }

  // 文件上传
  fileUpload(file: any) {
    return this.httpClient.post(this.serviceUrls.uploadFileUrl, file).toPromise();
  }

  // 查询会员开通记录
  getMemberList(params: any) {
    return this.httpClient.get(this.serviceUrls.memberList, {params}).toPromise();
  }

  // 会员开通取消订单
  memberCancel(id: any) {
    return this.httpClient.post(this.serviceUrls.memberCancel, {id}).toPromise();
  }

  // 查询会员开通记录详情
  getMemberDetail(id: any) {
    return this.httpClient.get(this.serviceUrls.memberDetail + id).toPromise();
  }

  // 查询会员开通记录售后退款
  memberRefund(body: any) {
    return this.httpClient.post(this.serviceUrls.memberRefund, body).toPromise();
  }

  // 查询会员开通记录详情
  memberExport(params: any) {
    window.open(this.serviceUrls.memberExport +
      `?page=${params.page}&
pageSize=${params.pageSize}&
userId=${params.userId}&
orderNum=${params.orderNum}&
goodsType=${params.goodsType}&
status=${params.status}&
createBeginTime=${params.createBeginTime}&
createEndTime=${params.createEndTime}&
expireBeginTime=${params.expireBeginTime}&
expireEndTime=${params.expireEndTime}&
    `);
  }

  // 开通记录跟进
  memberTrack(body: any) {
    return this.httpClient.post(this.serviceUrls.memberTrack, body).toPromise();
  }

  // 获取跟进记录
  getTrackList(orderNum: any) {
    return this.httpClient.get(this.serviceUrls.memberTrack, {params: {type: '1', businessNo: orderNum}}).toPromise();
  }

  formatTime(date: any, type: 'date' | 'dateTime' | 'time') {
    if (!date || (typeof date) === 'string') {
      return date || '';
    }
    let result = '';
    switch (type) {
      case 'date':
        // @ts-ignore
        result = date.toISOString().match(/\d{4}-\d{2}-\d{2}/)[0];
        break;
      case 'dateTime':
        // @ts-ignore
        result = date.toISOString().match(/\d{4}-\d{2}-\d{2}/)[0] + ' ' + date.toString().match(/\d{2}:\d{2}:\d{2}/)[0];
        break;
      case 'time':
        // @ts-ignore
        result = date.toString().match(/\d{2}:\d{2}:\d{2}/)[0];
        break;
    }
    return result;
  }

}
